package com.ht.right.dao.impl;

import java.util.List;

import com.ht.common.entity.PageInfo;
import com.ht.right.dao.CommonDao;
import com.ht.right.util.CommonUtil;

public class CommonDaoImpl<T> extends BaseDaoImpl<T> implements CommonDao<T>{
	@Override
	public List<T> loadAllList(String entityClsName) {
		String hql = "from " + entityClsName;
		return super.find(hql);
	}

	@Override
	public int getAllDataCount(String entityClsName, String docIds) {
		StringBuffer hql = new StringBuffer("select count(*) from ");
		hql.append(entityClsName);
		if(CommonUtil.isNullOrEmpty(docIds)){
			return 0;
		}else{
			hql.append(" where uuid in ("+docIds+")");
		}
		return Integer.parseInt(super.findUnique(hql.toString()).toString());
	}


	@Override
	public List<T> getPageDataList(String entityClsName, PageInfo pageInfo, String docIds) {
		StringBuffer hql = new StringBuffer("from ");
		hql.append(entityClsName);
		
		if(CommonUtil.isNullOrEmpty(docIds)){
			return null;
		}else{
			hql.append(" where uuid in ("+docIds+")");
		}
		
		if(CommonUtil.isNullOrEmpty(pageInfo)){
			return super.find(hql.toString(), new String[] {},0, 0);
		}else{
			if (CommonUtil.isNullOrEmpty(pageInfo.getSortField())) {
			} else {
				hql.append(" order by ");
				hql.append(pageInfo.getSortField());
			}
			if (CommonUtil.isNullOrEmpty(pageInfo.getSortOrder())) {
			} else {
				hql.append(" ");
				hql.append(pageInfo.getSortOrder());
			}
		}
		return super.find(hql.toString(), new String[] {}, pageInfo.getpageIndex(), pageInfo.getPageSize());
		
	}

	@Override
	public T getEntityByID(String entityClsName, String uuid) {
		String hql = "from " + entityClsName+" where uuid='"+uuid+"'";
		return super.get(hql, new String[]{});
	}
	
	
	
}
